package com.gm.wj.dao;

import com.gm.wj.entity.PtCaptain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface PtCaptainDAO extends JpaRepository<PtCaptain,Integer> {

    // 根据商品id 查询状态为1:拼团中，2:拼团成功的商品
    @Query(value = "SELECT * FROM pt_captain WHERE product_id = ?1 and (pt_status = 1 or pt_status = 2)  order by id desc ",nativeQuery = true)
    List<PtCaptain> findByPtStatusIng(String productId);

    PtCaptain findByFatherId(String key);

    // 设置拼团团单状态
    @Transactional
    @Query(value = "update pt_captain set pt_status=?2 where id=?1 ", nativeQuery = true)
    @Modifying
    void updateStatus(int id,int key);

    PtCaptain findByOrderId(String out_trade_no);

    @Query(value = "select COUNT(1) from pt_captain where order_id = ?1 ",nativeQuery = true)
    int findPtCaptainCountByOrderId(String out_trade_no);

}
